package com.mew.slots.core.game.jg.service;

import com.alibaba.fastjson.JSON;
import com.mew.common.entity.game.WinFree;
import com.mew.slots.core.common.game.BaseGameService;
import com.mew.slots.core.common.game.vo.BasePlayMsgs;
import com.mew.slots.core.common.messge.response.SlotsPlayResponseMessage;
import com.mew.slots.core.common.vo.StrategyGameVO;
import com.mew.slots.core.game.jg.bo.JgInRoom;
import com.mew.slots.core.game.jg.bo.JgInRoomList;
import com.mew.slots.core.game.jg.bo.JgPlayMsgs;
import com.mew.slots.core.game.jg.message.JgResponseMessage;
import com.mew.slots.core.game.jg.roller.JgXml;
import com.mew.slots.core.game.jg.room.JgRoom;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @author Dylin
 */
@Service
@Slf4j
public class JgService extends BaseGameService {
    @Override
    public BasePlayMsgs startGame(StrategyGameVO player) {
        JgInRoomList inList;
        if (StringUtils.isNotEmpty(player.getWinFree().getJson())){
            try {
                inList = JSON.parseObject(player.getWinFree().getJson(), JgInRoomList.class);
            }catch (Exception e){
                log.debug("WinFree json解析出错：{}",player.getWinFree().getJson());
                inList = new JgInRoomList();
            }
        }else{
            inList = new JgInRoomList();
        }
        JgInRoom in = null;
        for (JgInRoom jgInRoom : inList.getInRoomList()) {
            if(jgInRoom.getBet() == player.getInBetNumber()){
                in = jgInRoom;
            }
        }
        if(null == in){
            in = new JgInRoom();
            in.setBet(player.getInBetNumber());
            inList.getInRoomList().add(in);
        }
        JgRoom room = new JgRoom(winFree.getFreeRate(),in);
        room.init(player.getRollerId(), JgXml.getInstance(),player.getWinFree().getFrees()>0,player.getInBetNumber(),player.getControlType());
        room.roundPlay();
        player.getWinFree().setJson(JSON.toJSONString(inList));
        return room.getRes();
    }

    @Override
    public SlotsPlayResponseMessage myResp(WinFree winFree, BasePlayMsgs res) {
        JgResponseMessage message = new JgResponseMessage();
        JgPlayMsgs pmres = (JgPlayMsgs) res;

        message.setFreeRate(pmres.getFreeRate());
        message.setQw(pmres.getQw());
        message.setQwWin(pmres.getQwWin());
        message.setEy(pmres.getEy());
        message.setEyWin(pmres.getEyWin());
        message.setBz(pmres.getBz());
        message.setBzWin(pmres.getBzWin());
        message.setJg(pmres.getJg());
        message.setJgWin(pmres.getJgWin());

        if(pmres.getFree()>0 && winFree.getFreeNumber() == 0){
            winFree.setFreeRate(pmres.getFreeRate());
        }
        if(pmres.getFree() == 0 && winFree.getFrees() >0  && winFree.getFreeNumber() == 1){
            winFree.setFreeRate(0);
        }
        return message;
    }
    @Override
    public void myAfterResp(SlotsPlayResponseMessage message,StrategyGameVO player, BasePlayMsgs res) { }
}
